C API Reference
This document covers the mruby C API for embedding and extending mruby.
Contents: Headers | State Management | Values | Defining Classes and Modules | Defining Methods | Parsing Arguments | Calling Ruby Methods from C | String Operations | Array Operations | Hash Operations | Wrapping C Structures | Exception Handling | Method Visibility | Proc and Block Handling | Fiber API | Compilation Contexts | Precompiled Bytecode | GC Arena | Memory Allocation
Headers
State Management
mrb_close(mrb);
mrb_state * mrb_open(void)
Creates new mrb_state.
Definition state.c:77
mrb_state * mrb_open_core(void)
Create new mrb_state with just the mruby core.
Definition state.c:44
mrb_open() returns NULL on allocation failure. Always check the return value.
Values
All Ruby values are represented as mrb_value in C.
Creating Values
mrb_bool_value(mrb_bool b)
mrb_fixnum_value(mrb_int i)
mrb_obj_value(void *p)
static mrb_value mrb_true_value(void)
Returns true in Ruby.
Definition value.h:413
static mrb_value mrb_false_value(void)
Returns false in Ruby.
Definition value.h:402
static mrb_value mrb_nil_value(void)
Get a nil mrb_value object.
Definition value.h:391
static mrb_value mrb_float_value(mrb_state *mrb, mrb_float f)
Returns a float in Ruby.
Definition value.h:331
Type Checking
mrb_type(v)
mrb_nil_p(v)
mrb_integer_p(v)
mrb_float_p(v)
mrb_symbol_p(v)
mrb_string_p(v)
mrb_array_p(v)
mrb_hash_p(v)
mrb_true_p(v)
mrb_false_p(v)
mrb_undef_p(v)
mrb_immediate_p(v)
Extracting C Values
mrb_integer(v)
mrb_float(v)
mrb_symbol(v)
mrb_ptr(v)
mrb_str_to_cstr(mrb, v)
Value Types
| mrb_vtype | Ruby Class | Notes |
| MRB_TT_FALSE | FalseClass/NilClass | nil has MRB_TT_FALSE |
| MRB_TT_TRUE | TrueClass | |
| MRB_TT_INTEGER | Integer | Immediate value |
| MRB_TT_FLOAT | Float | May be immediate |
| MRB_TT_SYMBOL | Symbol | Immediate value |
| MRB_TT_STRING | String | Heap object |
| MRB_TT_ARRAY | Array | Heap object |
| MRB_TT_HASH | Hash | Heap object |
| MRB_TT_OBJECT | Object | User-defined classes |
| MRB_TT_CLASS | Class | |
| MRB_TT_MODULE | Module | |
| MRB_TT_PROC | Proc | |
| MRB_TT_CDATA | (C data) | Wrapped C structs |
| MRB_TT_EXCEPTION | Exception | |
| MRB_TT_FIBER | Fiber | |
Defining Classes and Modules
struct RClass *my_class = mrb_define_class(mrb,
"MyClass", mrb->object_class);
struct RClass *inner = mrb_define_class_under(mrb, outer,
"Inner", mrb->object_class);
struct RClass *my_mod = mrb_define_module(mrb,
"MyModule");
struct RClass *inner_mod = mrb_define_module_under(mrb, outer,
"InnerMod");
mrb_include_module(mrb, my_class, my_mod);
mrb_prepend_module(mrb, my_class, my_mod);
struct RClass *c = mrb_class_get(mrb,
"String");
struct RClass *m = mrb_module_get(mrb,
"Kernel");
mrb_define_const(mrb, my_class, "VERSION", mrb_str_new_lit(mrb, "1.0"));
Class class.
Definition class.h:17
Defining Methods
All C methods have the same signature:
{
}
Definition boxing_nan.h:40
Register with:
mrb_define_method(mrb, klass,
"name", my_method,
MRB_ARGS_NONE());
mrb_define_class_method(mrb, klass,
"name", my_method,
MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mod,
"name", my_method,
MRB_ARGS_ANY());
#define MRB_ARGS_ANY()
Function accepts any number of arguments.
Definition mruby.h:960
#define MRB_ARGS_NONE()
Function accepts no arguments.
Definition mruby.h:965
#define MRB_ARGS_REQ(n)
Function requires n arguments.
Definition mruby.h:918
Argument Specifiers
These can be combined with |:
#define MRB_ARGS_OPT(n)
Function takes n optional arguments.
Definition mruby.h:926
#define MRB_ARGS_BLOCK()
Function takes a block argument.
Definition mruby.h:950
Parsing Arguments
mrb_get_args() extracts arguments from the Ruby call stack:
mrb_int mrb_get_args(
mrb_state *mrb,
const char *format, ...);
Format Specifiers
| Spec | Ruby Type | C Type(s) | Notes |
| o | any | mrb_value | No type check |
| i | Numeric | mrb_int | Coerces to integer |
| f | Numeric | mrb_float | Coerces to float |
| b | any | mrb_bool | Truthiness |
| n | String/Symbol | mrb_sym | Converts to symbol |
| s | String | const char*, mrb_int | Pointer + length |
| z | String | const char* | Null-terminated |
| S | String | mrb_value | String value |
| A | Array | mrb_value | Array value |
| H | Hash | mrb_value | Hash value |
| C | Class | mrb_value | Class/Module value |
| c | Class | struct RClass* | Class pointer |
| a | Array | const mrb_value*, mrb_int | Array pointer + length |
| d | C Data | void* | Requires mrb_data_type* |
| & | Block | mrb_value | Block argument |
| * | rest | const mrb_value*, mrb_int | Rest arguments |
| \| | — | — | Following args are optional |
| ? | — | mrb_bool | Was previous optional arg given? |
| : | keywords | mrb_kwargs | Keyword arguments |
Adding ! to S, A, H, C, c, s, z, a, d allows nil (returns NULL/zero for nil).
Examples
const char *name; mrb_int len, count;
mrb_get_args(mrb, "si", &name, &len, &count);
mrb_get_args(mrb, "o|o", &req, &opt);
mrb_get_args(mrb, "*", &args, &argc);
mrb_get_args(mrb, "&", &block);
mrb_sym kw_names[] = { mrb_intern_lit(mrb,
"name"), mrb_intern_lit(mrb,
"age") };
mrb_kwargs kw = { 2, 1, kw_names, kw_values, NULL };
mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format,...)
Retrieve arguments from mrb_state.
Definition class.c:1970
Calling Ruby Methods from C
mrb_funcall(mrb, obj, "method", 2, arg1, arg2);
mrb_funcall_id(mrb, obj, mrb_intern_lit(mrb, "method"), 2, arg1, arg2);
mrb_value mrb_funcall_argv(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv)
Call existing Ruby functions.
Definition vm.c:889
mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv)
Yields to a block with an array of arguments.
Definition vm.c:1320
mrb_value mrb_funcall_with_block(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv, mrb_value block)
Call existing Ruby functions with a block.
Definition vm.c:801
mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg)
Yields to a block with a single argument.
Definition vm.c:1347
String Operations
mrb_str_new_lit(mrb, "hello")
mrb_str_new(mrb, ptr, len)
mrb_str_new_cstr(mrb, cstr)
mrb_str_new_static(mrb, ptr, len)
RSTRING_PTR(str)
RSTRING_LEN(str)
mrb_str_to_cstr(mrb, str)
mrb_str_cat(mrb, str, ptr, len)
mrb_str_cat_cstr(mrb, str, cstr)
mrb_str_cat_str(mrb, str, str2)
mrb_str_equal(mrb, str1, str2)
mrb_str_cmp(mrb, str1, str2)
Array Operations
mrb_ary_new(mrb)
mrb_ary_new_capa(mrb, capa)
mrb_ary_new_from_values(mrb, n, vals)
RARRAY_PTR(ary)
RARRAY_LEN(ary)
mrb_ary_entry(ary, idx)
mrb_ary_push(mrb, ary, val)
mrb_ary_pop(mrb, ary)
mrb_ary_shift(mrb, ary)
mrb_ary_unshift(mrb, ary, val)
mrb_ary_set(mrb, ary, idx, val)
mrb_ary_splice(mrb, ary, pos, len, rpl)
mrb_ary_concat(mrb, ary, other)
Hash Operations
mrb_hash_new(mrb)
mrb_hash_get(mrb, hash, key)
mrb_hash_fetch(mrb, hash, key, def)
mrb_hash_key_p(mrb, hash, key)
mrb_hash_empty_p(mrb, hash)
mrb_hash_size(mrb, hash)
mrb_hash_set(mrb, hash, key, val)
mrb_hash_delete_key(mrb, hash, key)
mrb_hash_merge(mrb, hash1, hash2)
mrb_hash_keys(mrb, hash)
mrb_hash_values(mrb, hash)
Wrapping C Structures
To expose a C struct to Ruby, use mrb_data_type and Data_Wrap_Struct:
static void point_free(
mrb_state *mrb,
void *p) {
mrb_free(mrb, p);
}
"Point", point_free
};
{
mrb_float x, y;
double *data = (double*)mrb_malloc(mrb, sizeof(double) * 2);
data[0] = x;
data[1] = y;
DATA_PTR(self) = data;
DATA_TYPE(self) = &point_type;
return self;
}
{
double *data = (double*)mrb_data_get_ptr(mrb, self, &point_type);
}
MRB_SET_INSTANCE_TT(point, MRB_TT_CDATA);
struct RClass * mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super)
Defines a new class.
Definition class.c:606
void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec)
Defines a global function in Ruby.
Definition class.c:1113
Custom C wrapped data.
Definition data.h:22
Do not call into mruby from a dfree handler. The handler runs from inside GC sweep; allocating Ruby objects, calling mrb_funcall, mrb_yield, raising exceptions, or otherwise re-entering the VM can trigger a recursive GC that revisits the same object and causes double-free. Keep dfree to mrb_free / plain C cleanup of the wrapped data only.
Exception Handling
Raising Exceptions
mrb_raise(mrb, E_RUNTIME_ERROR, "something went wrong");
mrb_raisef(mrb, E_ARGUMENT_ERROR, "expected %d, got %d", expected, actual);
mrb_raise(mrb, E_TYPE_ERROR, "wrong type");
Common exception classes: E_RUNTIME_ERROR, E_TYPE_ERROR, E_ARGUMENT_ERROR, E_RANGE_ERROR, E_NAME_ERROR, E_NOMETHOD_ERROR, E_NOTIMP_ERROR, E_KEY_ERROR.
Catching Exceptions
mrb_value result = mrb_load_string(mrb, code);
if (mrb->exc) {
mrb_print_error(mrb);
mrb->exc = NULL;
}
Protected Execution
mrb_protect() executes a function under protection. If an exception is raised, it is captured as a return value instead of propagating:
{
return mrb_funcall(mrb, data, "do_something", 0);
}
mrb_bool error;
if (error) {
mrb_value inspect = mrb_inspect(mrb, result);
}
mrb_value mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state)
Protect (takes mrb_value for body argument).
char * mrb_str_to_cstr(mrb_state *mrb, mrb_value str)
Returns a newly allocated C string from a Ruby string.
Definition string.c:1085
Note: mrb_protect clears mrb->exc after catching the exception. The exception is returned as result. Do not use mrb_print_error() after mrb_protect — it reads mrb->exc which is already NULL.
For lower-level protection with a void* callback:
{
}
mrb_bool error;
mrb_value mrb_protect_error(mrb_state *mrb, mrb_protect_error_func *body, void *userdata, mrb_bool *error)
Protects a C function call from mruby exceptions.
Definition vm.c:540
Rescue
mrb_rescue() catches StandardError (like Ruby's rescue):
{
return mrb_funcall(mrb, body_data, "risky_method", 0);
}
{
}
rescue_func, rescue_data);
mrb_value mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data)
Rescue.
To rescue specific exception classes:
E_ARGUMENT_ERROR,
mrb_class_get(mrb, "IOError")
};
rescue_func, rescue_data,
2, classes);
mrb_value mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data, mrb_int len, struct RClass **classes)
Rescue exception.
Ensure
mrb_ensure() guarantees cleanup runs regardless of exceptions (like Ruby's ensure):
{
return mrb_funcall(mrb, data, "process", 0);
}
{
}
cleanup_func, cleanup_data);
mrb_value mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t ensure, mrb_value e_data)
Ensure.
mrb_value mrb_funcall(mrb_state *mrb, mrb_value val, const char *name, mrb_int argc,...)
Call existing Ruby functions.
Definition vm.c:605
The ensure function always executes. If the body raises an exception, the ensure runs and then the exception is re-raised.
Error State Management
Method Visibility
mrb_define_private_method(mrb, klass,
"name", func,
MRB_ARGS_NONE());
mrb_define_class_method(mrb, klass,
"name", func,
MRB_ARGS_NONE());
mrb_define_module_function(mrb, mod,
"name", func,
MRB_ARGS_NONE());
mrb_define_singleton_method(mrb, obj,
"name", func,
MRB_ARGS_NONE());
mrb_define_alias(mrb, klass, "new_name", "old_name");
mrb_undef_method(mrb, klass, "name");
mrb_undef_class_method(mrb, klass, "name");
All _method variants have _id counterparts that accept mrb_sym instead of const char* for better performance.
Proc and Block Handling
Creating Procs from C Functions
struct RProc *proc = mrb_proc_new_cfunc(mrb, my_func);
struct RProc *proc = mrb_closure_new_cfunc(mrb, my_func, nlocals);
C Functions with Environment (requires mruby-proc-ext)
Store values in a proc's environment, accessible from the C function:
mrb_value env_values[] = { mrb_fixnum_value(42) };
struct RProc *proc = mrb_proc_new_cfunc_with_env(mrb, my_func, 1, env_values);
{
mrb_value val = mrb_proc_cfunc_env_get(mrb, 0);
return val;
}
Fiber API
Creating and Using Fibers
mrb_value mrb_fiber_new(mrb_state *mrb, const struct RProc *proc)
Create a new Fiber from proc object.
mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib)
Check if a Fiber is alive.
mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv)
Resume a Fiber.
Yielding from C
mrb_fiber_yield() can only be used as the return value of a C function — no code may execute after it:
{
mrb_value yield_args[] = { mrb_str_new_lit(mrb,
"yielded") };
}
mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv)
Yield a Fiber.
Fiber States
| State | Meaning |
| MRB_FIBER_CREATED | Created but not yet resumed |
| MRB_FIBER_RUNNING | Currently executing |
| MRB_FIBER_RESUMED | Resumed another fiber |
| MRB_FIBER_SUSPENDED | Yielded, waiting to resume |
| MRB_FIBER_TRANSFERRED | Transferred via Fiber#transfer |
| MRB_FIBER_TERMINATED | Finished execution |
Limitation: fibers cannot yield across C function boundaries. You cannot call mrb_fiber_yield from within a C-implemented method, except via mrb_fiber_yield at function return.
Compilation Contexts
For advanced compilation control, use mrb_ccontext:
mrb_ccontext_filename(mrb, cxt, "my_script.rb");
mrb_value result = mrb_load_string_cxt(mrb,
"1 + 2", cxt);
mrb_ccontext_free(mrb, cxt);
Context Options
The mrb_ccontext structure provides several flags:
| Field | Purpose |
| capture_errors | Collect parse errors instead of raising |
| no_exec | Compile without executing (get RProc) |
| no_optimize | Disable peephole optimizations |
| no_ext_ops | Disable extended operand instructions |
| keep_lv | Preserve local variables across loads |
Loading with Context
mrb_load_string_cxt(mrb, code, cxt);
mrb_load_nstring_cxt(mrb, code, len, cxt);
mrb_load_file_cxt(mrb, fp, cxt);
mrb_load_detect_file_cxt(mrb, fp, cxt);
Precompiled Bytecode
Load .mrb files compiled by mrbc:
mrb_value result = mrb_load_irep(mrb, bytecode);
mrb_value result = mrb_load_irep_buf(mrb, buf, size);
FILE *fp = fopen("script.mrb", "rb");
mrb_value result = mrb_load_irep_file(mrb, fp);
fclose(fp);
mrb_irep *irep = mrb_read_irep(mrb, bytecode);
All _irep loading functions have _cxt variants that accept a compilation context.
Deployment Pattern
Ahead-of-time compilation eliminates the need for the compiler gem at runtime:
# Compile to C array
mrbc -Bscript_bytecode script.rb
# This generates a C header with:
# const uint8_t script_bytecode[];
#include "script.mrb.h"
mrb_load_irep(mrb, script_bytecode);
Important: wrap bytecode loading in arena save/restore when loading multiple scripts:
int ai = mrb_gc_arena_save(mrb);
mrb_load_irep(mrb, script1);
mrb_gc_arena_restore(mrb, ai);
ai = mrb_gc_arena_save(mrb);
mrb_load_irep(mrb, script2);
mrb_gc_arena_restore(mrb, ai);
Symbols
mrb_sym sym = mrb_intern_lit(mrb,
"name");
mrb_sym sym = mrb_intern_cstr(mrb, cstr);
mrb_sym sym = mrb_intern(mrb, ptr, len);
const char *name = mrb_sym_name(mrb, sym);
mrb_int len;
const char *name = mrb_sym_name_len(mrb, sym, &len);
Instance Variables
mrb_iv_get(mrb, obj, mrb_intern_lit(mrb, "@x"));
mrb_iv_set(mrb, obj, mrb_intern_lit(mrb, "@x"), val);
mrb_iv_defined(mrb, obj, mrb_intern_lit(mrb, "@x"));
mrb_iv_remove(mrb, obj, mrb_intern_lit(mrb, "@x"));
Global Variables
mrb_gv_get(mrb, mrb_intern_lit(mrb, "$verbose"));
Class Variables
mrb_cv_get(mrb, klass, mrb_intern_lit(mrb, "@@count"));
mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "@@count"), mrb_fixnum_value(0));
Loading and Executing Code
mrb_value result = mrb_load_string(mrb,
"1 + 2");
FILE *f = fopen("script.rb", "r");
fclose(f);
mrb_value result = mrb_load_irep(mrb, bytecode_array);
mrb_value mrb_load_file(mrb_state *, FILE *)
program load functions
GC Arena
When creating many temporary Ruby objects in C, use the GC arena to prevent them from being collected prematurely:
int ai = mrb_gc_arena_save(mrb);
mrb_gc_arena_restore(mrb, ai);
See gc-arena-howto.md for details.
Memory Allocation
void *p = mrb_malloc(mrb, size);
void *p = mrb_calloc(mrb, nmemb, size);
void *p = mrb_realloc(mrb, ptr, size);
mrb_free(mrb, p);
void *p = mrb_malloc_simple(mrb, size);
void *p = mrb_realloc_simple(mrb, ptr, size);
Type Conversion
mrb_obj_as_string(mrb, val)
mrb_inspect(mrb, val)
mrb_any_to_s(mrb, val)
mrb_str_to_integer(mrb, str, base, badcheck)
mrb_str_to_dbl(mrb, str, badcheck)
mrb_ensure_float_type(mrb, val)
Object Comparison
mrb_equal(mrb, a, b)
mrb_eql(mrb, a, b)
mrb_obj_eq(mrb, a, b)
mrb_cmp(mrb, a, b)
Object Inspection
mrb_obj_classname(mrb, obj)
mrb_obj_class(mrb, obj)
mrb_obj_is_kind_of(mrb, obj, klass)
mrb_obj_respond_to(mrb, klass, mid)
mrb_obj_id(obj)
mrb_obj_freeze(mrb, obj)
mrb_obj_dup(mrb, obj)
Compile-Time Flags
When compiling C code that uses mruby, you must use the same flags as the library was built with. Use mruby-config to get them:
$ build/host/bin/mruby-config --cflags # compiler flags
$ build/host/bin/mruby-config --ldflags # linker flags
$ build/host/bin/mruby-config --libs # libraries
Key macros that affect ABI:
| Macro | Effect |
| MRB_NO_BOXING | Struct-based values (larger, debuggable) |
| MRB_WORD_BOXING | Single-word values (fast, 32-bit safe) |
| MRB_NAN_BOXING | NaN-tagged values (default on 32-bit) |
| MRB_NO_FLOAT | Disable Float support |
| MRB_INT64 | 64-bit integers |
| MRB_USE_FLOAT32 | 32-bit floats |
Mismatching these between library and application causes silent data corruption.